<?php
require_once ("global.php");
$act = trim($_POST ['act']);
include("../upload.class.php");
$CONF['jpeg_enable']   = '1';
$CONF['gif_enable']   = '1';
$CONF['png_enable']   = '1';
function extend_2($file_name)   
{   
$extend = pathinfo($file_name);   
$extend = strtolower($extend["extension"]);   
return $extend;   
} 

function check_addslashes($content){
	if(get_magic_quotes_gpc()==0){
	    $content = addslashes($content);
	}
	return $content;
}

$_POST ['content']=check_addslashes($_POST ['content']);

//添加
if ($act=='add') {
	if(empty($_POST['title'])){
		exit("<script>alert('标题不能为空!');window.history.go(-1)</script>");
	}
	if(empty($_POST['cid'])){
		exit("<script>alert('栏目不能为空!');window.history.go(-1)</script>");
	}
	$record = array(
		'cid'			=>$_POST ['cid'],
		'title'			=>$_POST ['title'],
		'subtitle'		=>$_POST ['subtitle'],
		'ontop'			=>$_POST ['ontop'],
		'hot'			=>$_POST ['hot'],
		//'att'			=>is_array($_POST ['att'])?implode(',',$_POST ['att']):'',
		'source'		=>$_POST ['source'],
		'author'		=>$_POST ['author'],
		'resume'		=>$_POST ['resume'],
		'content'		=>$_POST ['content'],
		'pubdate'		=>date ( "Y-m-d H:i:s" ),
		'created_date'	=>date ( "Y-m-d H:i:s" ),
		'created_by'	=>$_COOKIE['userid']	
	);
	if(!empty($_FILES['pic']['name'])){
		//$upload_file = uploadFile('pic');//上传图片，返回地址
		if(extend_2($_FILES['pic']['name'])=="gif"||extend_2($_FILES['pic']['name'])=="jpg"||extend_2($_FILES['pic']['name'])=="png"){
			$prefix='pic_'.time().mt_rand(1,99999);
			$f_upload = new upload_file($prefix,"php,exe,rar,txt",'../../upload',512,$_FILES['pic']['type']);         
			$f_upload->set_file_name_size($_FILES['pic']['name'],$_FILES['pic']['size']);
			$f_upload->set_upfile($_FILES['pic']['tmp_name']); 
			$upload_file = $f_upload->do_save();
		}
		$record['pic']=$upload_file;
	}
	//print_r($record);exit;
	$id = $db->save('phpaadb_article',$record);
	admin_log('新增文章');
	echo "<script language=javascript>window.location='article.php?id=".$_POST['cid']."';</script>";
}

//修改
if ($act=='edit'){
	$id = $_POST ['id'];
	if(empty($_POST['title'])){
		exit("<script>alert('标题不能为空!');window.history.go(-1)</script>");
	}
	if(empty($_POST['cid'])){
		exit("<script>alert('栏目不能为空!');window.history.go(-1)</script>");
	}	
	
	$record = array(
		'cid'			=>$_POST ['cid'],
		'title'			=>$_POST ['title'],
		'subtitle'		=>$_POST ['subtitle'],
		'ontop'			=>$_POST ['ontop'],
		'hot'			=>$_POST ['hot'],
		//'att'			=>is_array($_POST ['att'])?implode(',',$_POST ['att']):'',
		'source'		=>$_POST ['source'],
		'author'		=>$_POST ['author'],
		'resume'		=>$_POST ['resume'],
		'content'		=>$_POST ['content'],
		'pubdate'	=>date ( "Y-m-d H:i:s" ),
		'created_date'	=>date ( "Y-m-d H:i:s" ),
		'created_by'	=>$_COOKIE['userid']	
	);
	if(!empty($_FILES['pic']['name'])){
		if(extend_2($_FILES['pic']['name'])=="gif"||extend_2($_FILES['pic']['name'])=="jpg"||extend_2($_FILES['pic']['name'])=="png"){
			$prefix='pic_'.time().mt_rand(1,99999);
			$f_upload = new upload_file($prefix,"php,exe,rar,txt",'../../upload',512,$_FILES['pic']['type']);         
			$f_upload->set_file_name_size($_FILES['pic']['name'],$_FILES['pic']['size']);
			$f_upload->set_upfile($_FILES['pic']['tmp_name']); 
			$upload_file = $f_upload->do_save();
		}
		$record['pic']=$upload_file;
	}
	$db->update('phpaadb_article',$record,'id='.$id);
	admin_log('修改文章');
	echo "<script language=javascript>window.location='article.php?id=".$_POST['cid']."';</script>";
}

//删除
if ($act=='delete') {	
	$id = $_POST ['id'];
	admin_log('删除文章');
	$db->update('phpaadb_article',array('delete_session_id'=>$_COOKIE['userid']),'id in('.$id.')');
	exit();
}

//转移文章
if ($act=='move') {	
	$scid =$_POST['scid'];
	$id = $_POST ['id'];
	admin_log('转移文章');
	$db->update('phpaadb_article',array('cid'=>$scid),'id in('.$id.')');
	exit();
}

//删除缩略图
if ($act=='delpic') {
	$id = $_POST ['id'];
	$pic_path = $db->getOneField("select pic from phpaadb_article where id=".$id);
	$pic_path = $db->getOneField("select pic from phpaadb_article where id=".$id);
	if(is_file(ROOT_PATH.$pic_path)){
		@unlink(ROOT_PATH.$pic_path);
	}
	$db->update('phpaadb_article',array('pic'=>''),'id in('.$id.')');
	exit();
}

//彻底删除垃圾
if ($act=='cdelete') {	
	$id = $_POST ['id'];
	$db->delete('phpaadb_article','id in('.$id.')');
	exit();
}

//还原垃圾
if ($act=='revert') {	
	$id = $_POST ['id'];
	$db->query("UPDATE phpaadb_article set delete_session_id = null where id in (".$id.")");
}


?>